/*
 * Settings.java
 *
 * Created on May 18, 2006, 8:25 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package hubsniffer;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Console;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.Serializable;

/**
 *
 * @author root
 */
public class Settings implements Serializable {

    public static String SETTINGS_PATH = System.getProperty("user.dir") + File.separatorChar + ".hubsniffer";
    private static Settings settings = null;

    private static void createRootDir() {
        new File(SETTINGS_PATH).mkdir();
        new File(settings.getHUBSNIFFERLOG_PATH()).mkdir();
        new File(settings.getHUBLISTS_PATH()).mkdir();
        new File(settings.getRESOURCE_PATH()).mkdir();
        new File(settings.getLOG_PATH()).mkdir();
    }

    public static Settings getInstance() {
        if (settings == null) {
            settings = new Settings();
            try {
                settings.load();
                createRootDir();
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();

                settings.defaultSettings();
                createRootDir();
                try {
                    settings.save();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        return settings;
    }

    private void defaultSettings() {

        setHUBSNIFFERLOG_PATH(SETTINGS_PATH + File.separatorChar + "hubs");
        setHUBLISTS_PATH(SETTINGS_PATH + File.separatorChar + "hublists");
        setHUBSNIFFERLOG_FILE_NAME("sniffer.log");
        setLOG_PATH(SETTINGS_PATH + File.separatorChar + "log");

        setRESOURCE_PATH(SETTINGS_PATH + File.separatorChar + "resources");
        setENCONDING("ISO-8859-1");

        askDBInfo();
    }

    public static Settings getInstance(String path) throws FileNotFoundException {
        SETTINGS_PATH = path;
        getInstance().load();
        return settings;
    }
    private String LOG_PATH;
    private String HUBLISTS_PATH;
    private String HUBSNIFFERLOG_PATH;
    private String HUBSNIFFERLOG_FILE_NAME;
    private String RESOURCE_PATH;
    private String ENCONDING;
    private String DB_ADDRESS = "jdbc:postgresql://localhost:5432/sghdc/public";
    private String DB_USERNAME = "sghdc";
    private String DB_PASSWD = "secret";

    private void askDBInfo() {
        Console console = System.console();
        boolean verified = false;

        String address = DB_ADDRESS;
        String username = DB_USERNAME;
        String passwd = DB_PASSWD;
        String confirmPasswd = DB_PASSWD;

        console.writer().write("It seams that's the first time using this application so you nead to configure some Settings\n");

        while (!verified) {
            address = DB_ADDRESS;
            username = DB_USERNAME;
            passwd = DB_PASSWD;
            confirmPasswd = DB_PASSWD;

            console.writer().write("Configuring the Database...\n");
            address = console.readLine("Database address(" + address + "): ").trim();
            address = (address.length() == 0) ? DB_ADDRESS : address;
            username = console.readLine("username (" + username + "): ").trim();
            username = (username.length() == 0) ? DB_USERNAME : username;

            do {
                if (!passwd.equals(confirmPasswd)) {
                    console.writer().write("\nThe passwd are different, try again!\n");
                }
                passwd = console.readLine("passwd (" + passwd + "): ").trim();
                passwd = (passwd.length() == 0) ? DB_PASSWD : passwd;
                confirmPasswd = console.readLine("Confirm passwd (" + passwd + "): ").trim();
                confirmPasswd = (confirmPasswd.length() == 0) ? DB_PASSWD : confirmPasswd;
            } while (!passwd.equals(confirmPasswd));

            console.writer().write("Database configuration:\n");
            console.writer().write("\tAddress: " + address + "\n");
            console.writer().write("\tusername: " + username + "\n");
            console.writer().write("\tpasswd: " + passwd + "\n");
            verified = ("y".equalsIgnoreCase(console.readLine("\nConfirm configurations(y/n)?").trim())) ? true : false;
        }

        this.setDB_ADDRESS(address);
        this.setDB_USERNAME(username);
        this.setDB_PASSWD(passwd);

    }

    private void load() throws FileNotFoundException {
        XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
                new FileInputStream(SETTINGS_PATH + File.separator + "Settings.xml")));
        settings = (Settings) decoder.readObject();
        decoder.close();
    }

    public static void save() throws FileNotFoundException {
        XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
                new FileOutputStream(SETTINGS_PATH + File.separator + "Settings.xml")));
        encoder.writeObject(settings);
        encoder.close();
    }

    public static void main(String[] args) throws FileNotFoundException {
        Settings set = new Settings();
        set.askDBInfo();
    }

    public String getHUBSNIFFERLOG_PATH() {
        return HUBSNIFFERLOG_PATH;
    }

    public void setHUBSNIFFERLOG_PATH(String HUBSNIFFERLOG_PATH) {
        this.HUBSNIFFERLOG_PATH = HUBSNIFFERLOG_PATH;
    }

    public String getHUBSNIFFERLOG_FILE_NAME() {
        return HUBSNIFFERLOG_FILE_NAME;
    }

    public void setHUBSNIFFERLOG_FILE_NAME(String HUBSNIFFERLOG_FILE_NAME) {
        this.HUBSNIFFERLOG_FILE_NAME = HUBSNIFFERLOG_FILE_NAME;
    }

    public String getENCONDING() {
        return ENCONDING;
    }

    public void setENCONDING(String ENCONDING) {
        this.ENCONDING = ENCONDING;
    }

    public String getDB_ADDRESS() {
        return DB_ADDRESS;
    }

    public void setDB_ADDRESS(String DB_ADDRESS) {
        this.DB_ADDRESS = DB_ADDRESS;
    }

    public String getDB_USERNAME() {
        return DB_USERNAME;
    }

    public void setDB_USERNAME(String DB_USERNAME) {
        this.DB_USERNAME = DB_USERNAME;
    }

    public String getDB_PASSWD() {
        return DB_PASSWD;
    }

    public void setDB_PASSWD(String DB_PASSWD) {
        this.DB_PASSWD = DB_PASSWD;
    }

    public String getHUBLISTS_PATH() {
        return HUBLISTS_PATH;
    }

    public void setHUBLISTS_PATH(String HUBLISTS_PATH) {
        this.HUBLISTS_PATH = HUBLISTS_PATH;
    }

    public String getRESOURCE_PATH() {
        return RESOURCE_PATH;
    }

    public void setRESOURCE_PATH(String RESOURCE_PATH) {
        this.RESOURCE_PATH = RESOURCE_PATH;
    }

    public String getLOG_PATH() {
        return LOG_PATH;
    }

    public void setLOG_PATH(String LOG_PATH) {
        this.LOG_PATH = LOG_PATH;
    }
}
